/**
 * 视频详情
 * Create by Songlcy
 */
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_app_visitor/utils/options_utils.dart';
// import 'package:flutter_aliplayer/flutter_aliplayer.dart';
// import 'package:flutter_aliplayer/flutter_aliplayer_factory.dart';
import 'package:shimmer/shimmer.dart';
import 'package:scoped_model/scoped_model.dart';

import 'package:flutter_ijkplayer_nusafe/flutter_ijkplayer_nusafe.dart';

import './video_desc_tab_page.dart';
import './video_play_tab_page.dart';
import './video_plot_tab_page.dart';
import '../../utils/device_util.dart';
import '../../models/pood/video_model.dart';
import '../../constants/video_detail_tab.dart';
import '../../delegate/sliver_appbar_delegate.dart';
import '../../models/state_model/video_detail_state_model.dart';


class VideoDetailPage extends StatefulWidget {
  VideoModel videoItem;
  VideoDetailPage({required this.videoItem});

  @override
  State<StatefulWidget> createState() => _VideoDetailPageState();
}

class _VideoDetailPageState extends State<VideoDetailPage> {

  late IjkMediaController controller;


  late VideoDetailStateModel videoDetailStateModel;
  // late FlutterAliplayer fAliplayer;
  @override
  void initState() {
    super.initState();
    videoDetailStateModel = new VideoDetailStateModel();
    videoDetailStateModel.fetchVideoDetail(widget.videoItem.id);

    controller = IjkMediaController();

    // OptionUtils.addDefaultOptions(controller);
    // String sourceUrl = "http://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8";
    // String sourceUrl = "http://192.168.110.35:10089/60da8314f43aa40044394980/index.m3u8";
    // String sourceUrl = "https://www.sample-videos.com/video123/mp4/360/big_buck_bunny_360p_30mb.mp4";
    // String sourceUrl = "http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8";
    // String sourceUrl = "https://media001.geekbang.org/f433fd1ce5e84d27b1101f0dad72a126/de563bb4aba94b5f95f448b33be4dd9f-9aede6861be944d696fe365f3a33b7b4-sd.m3u8";

    String sourceUrl = "http://192.168.3.185:3000/api/m3u8/610b37196fa0460095954e25";
    Map<String,String> header = {"referer":"http://wuqian.com"};
    controller.setDataSource(DataSource.network(sourceUrl,headers: header), autoPlay: true);
  }
  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ScopedModel(
        model: videoDetailStateModel,
        child: Scaffold(
          body: DefaultTabController(
            length: VIDEO_DETAIL_TAB.length,
            child: NestedScrollView(
              headerSliverBuilder:
                  (BuildContext context, bool innerBoxIsScrolled) {
                return <Widget>[
                  // 标题
                  SliverAppBar(
                    floating: true,
                    pinned: true,
                    collapsedHeight: MediaQuery.of(context).size.width >
                    MediaQuery.of(context).size.height * 1.2?DeviceUtil.getScreenSize(context).height
                        :DeviceUtil.getScreenSize(context).width * 0.8,
                    flexibleSpace: IjkPlayer(
                      mediaController: controller,
                    ),
                  ),
                  // Tab
                  SliverPersistentHeader(
                    pinned: true,
                    delegate: SliverAppBarDelegate(TabBar(
                      labelColor: Theme.of(context).primaryColor,
                      labelStyle: TextStyle(fontSize: 16.5),
                      unselectedLabelColor: Color.fromARGB(255, 192, 193, 195),
                      indicatorColor: Theme.of(context).primaryColor,
                      indicatorWeight: 2.0,
                      tabs: VIDEO_DETAIL_TAB
                          .map<Tab>((item) => Tab(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            item["icon"],
                            SizedBox(width: 3.0),
                            Text(item["name"],
                                style: TextStyle(fontSize: 15.0))
                          ],
                        ),
                      ))
                          .toList(),
                    )),
                  )
                ];
              },
              body: VideoDetailContent(widget.videoItem),
            ),
          ),
        )
    );
  }
}

class VideoDetailContent extends StatelessWidget {
  final VideoModel video;
  VideoDetailContent(this.video);

  @override
  Widget build(BuildContext context) {
    return TabBarView(
      children: <Widget>[
        VideoDescTabPage(video),
        VideoPlayTabPage(),
        VideoPlotTabPage()
      ],
    );
  }
}
